package com.quad.innovators.salesease.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonValue;
import com.quad.innovators.salesease.exception.BusinessException;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;

/**
 * @author cheng
 */
@Getter
public enum TagTypeEnum implements IBaseEnum<Integer>{
    task_manage_tag(1, "任务管理"),
    SALES_ORDER_TAG(2, "销售单管理");

    //  Mybatis-Plus 提供注解表示插入数据库时插入该值
    @EnumValue
    private final Integer value;

    @JsonValue //  表示对枚举序列化时返回此字段
    private final String label;

    TagTypeEnum(Integer value, String label) {
        this.value = value;
        this.label = label;
    }

    static final Map<Integer, TagTypeEnum> map = new HashMap<>();

    static {
        TagTypeEnum[] values = values();
        for (TagTypeEnum value : values) {
            map.put(value.value, value);
        }
    }

    public static TagTypeEnum useCodeGetEnum(Integer code) {
        TagTypeEnum tagTypeEnum = map.getOrDefault(code, null);
        if (ObjectUtils.isNull(tagTypeEnum)){
            throw BusinessException.resultCode(ResultCode.NOT_FOUND);
        }
        return tagTypeEnum;
    }
}
